cc.Class({
    extends: cc.Component,

    properties: {
         // 主角跳跃高度
        jumpHeight: 0,
        // 主角跳跃持续时间
        jumpDuration: 0,
        // 最大移动速度
        maxMoveSpeed: 0,
        // 加速度
        accel: 0,
        //跳跃的声音
        jumpAudio: {
            default: null,
            url: cc.AudioClip
        },
        positonX : 0,
        positionY : 0,
        isSelf: false,
        meLabel:cc.Node,
        isSend:0,
        sendFrequance:0,
        direction:""
    },

    setJumpAction: function () {
        // 跳跃上升
        var jumpUp = cc.moveBy(this.jumpDuration, cc.p(0, this.jumpHeight)).easing(cc.easeCubicActionOut());
        // 下落
        var jumpDown = cc.moveBy(this.jumpDuration, cc.p(0, -this.jumpHeight)).easing(cc.easeCubicActionIn());
        //回调函数 动作结束时调用
        var callback = cc.callFunc(this.playJumpSound, this);
        // 不断重复
        return cc.repeatForever(cc.sequence(jumpUp, jumpDown, callback));
    },
    playJumpSound: function () {
        Global.audioPlay(this.jumpAudio);
    },
    //玩家自己的角色启动
    selfLoad: function () {
        // 初始化跳跃动作
        this.jumpAction = this.setJumpAction();
        this.node.runAction(this.jumpAction);
        // 初始化键盘输入监听
        this.setInputControl();
        //显示头上的"我"字
        this.meLabel.active = true;
    },
    //另一个玩家的左右移动靠接收服务器数据
    chanllengerLoad: function() {
        // 初始化跳跃动作
        this.jumpAction = this.setJumpAction();
        this.node.runAction(this.jumpAction);
    },
    onLoad: function () {

        // 加速度方向开关
        this.accLeft = false;
        this.accRight = false;
         //主角当前水平方向速度
        this.xSpeed = 0;

        this.positonX = this.node.x;
        this.positionY = this.node.y;

        //开启重力感应
         cc.systemEvent.setAccelerometerEnabled(true);
    },

    setInputControl: function () {
        // 添加键盘事件监听
        // 有按键按下时，判断是否是我们指定的方向控制键，并设置向对应方向加速
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        // 松开按键时，停止向该方向的加速
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);

        cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION,this.onDeviceMotion, this);
    },
    onKeyDown : function (event){
        switch(event.keyCode) {
            case cc.KEY.a:
                Global.playerSelf.accLeft = true;
                if (this.direction == "left") return;

                var sendData = {
                    type : "accChange",
                    left: 1,
                    right: 0
                };
                var sendString = JSON.stringify(sendData);
                Global.ws.send(sendString);
                this.direction = "left";
                break;
            case cc.KEY.d:
                Global.playerSelf.accRight = true;
                if (this.direction == "right") return;

                var sendData = {
                    type : "accChange",
                    left: 0,
                    right: 1
                };
                var sendString = JSON.stringify(sendData);
                Global.ws.send(sendString);
                this.direction = "right";
                break;
        }


    },
    onKeyUp : function (event){
        switch(event.keyCode) {
            case cc.KEY.a:
                Global.playerSelf.accLeft = false;
                break;
            case cc.KEY.d:
                Global.playerSelf.accRight = false;
                break;
        }
    },

    //根据手机的左右倾斜来设定往左跳还是往右跳
    onDeviceMotion: function(event) {
        if (event.acc.x > 0) {
             Global.playerSelf.accLeft = false;
             Global.playerSelf.accRight = true;
             //只有当前的加速度方向与之前的不同时 才发送消息
            if (this.direction == "right") return;
            var sendData = {
                type : "accChange",
                left: 0,
                right:1
            };
            var sendString = JSON.stringify(sendData);
            Global.ws.send(sendString);
            this.direction = "right"
        } else if(event.acc.x < 0){
             Global.playerSelf.accLeft = true;
             Global.playerSelf.accRight = false;

            if (this.direction == "left") return;
            var sendData = {
                type : "accChange",
                left: 1,
                right:0
            };
            var sendString = JSON.stringify(sendData);
            Global.ws.send(sendString);
            this.direction = "left"
        }

    },
    gameOver: function () {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotion, this);
        this.node.stopAction(this.jumpAction);
        this.xSpeed = 0;
        this.accLeft = false;
        this.accRight = false;
        this.resetPostion();

    },
    update: function (dt) {
        // 根据当前加速度方向每帧更新速度
        if (this.accLeft == true) {
            this.xSpeed -= this.accel * dt;
        } else if (this.accRight) {
            this.xSpeed += this.accel * dt;
        }
        // 限制主角的速度不能超过最大值
        if ( Math.abs(this.xSpeed) > this.maxMoveSpeed ) {
            this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
        }

        // 根据当前速度更新主角的位置
        this.node.x += this.xSpeed * dt;

        //不能移动超出屏幕边界
        if (this.node.x > Global.game.node.width/2){
            this.node.x =  Global.game.node.width/2;
            this.xSpeed = 0;
        }
        if (this.node.x < -Global.game.node.width/2) {
            this.node.x = - Global.game.node.width/2;
            this.xSpeed = 0;
        }
        //游戏结束 不再发送角色位置信息
        if (Global.gameover == true) return;
        //减少发送位置信息的频率 在满足视觉不失真的前提下尽量少发送
        this.isSend += dt;
        if (this.isSend < this.sendFrequance){
            return;
        }
        this.isSend = 0;
        //this.sendPosition();

    },
    resetPostion: function() {
        this.node.setPosition(cc.p(this.positonX, this.positionY));
        //this.sendPosition();
    },
    sendPosition: function () {
        var sendData = {
            type : "positionChange",
            x : this.node.x,
            y : this.node.y
        };
        var sendString = JSON.stringify(sendData);
        Global.ws.send(sendString);
    }
});
